//
//  PlayerTimePicker.m
//  SleepingElf
//
//  Created by karong on 2023/10/19.
//

#import "PlayerTimePicker.h"

@interface PlayerTimePicker () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainViewBottom;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIPickerView *leftPicker;
@property (weak, nonatomic) IBOutlet UIPickerView *rightPicker;
/** 选择的时 */
@property (nonatomic, assign)NSInteger selectHour;
/** 选择的分 */
@property (nonatomic, assign)NSInteger selectMin;
@end

@implementation PlayerTimePicker

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.selectHour = 0;
    self.selectMin = 0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.leftPicker.subviews[1].backgroundColor = UIColor.clearColor;
        self.rightPicker.subviews[1].backgroundColor = UIColor.clearColor;
    });
    
}

- (IBAction)saveAction:(id)sender {
    NSInteger selectTime = self.selectHour * 60 + self.selectMin;
    if (selectTime > 0) {
        appConfig.sleepConfig.autoCloseTimeType = 4;
        appConfig.sleepConfig.autoCloseTime = selectTime;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)closeAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == self.leftPicker) {
        return 24;
    }
    else {
        return 60;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 40;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 50;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UIColor * color = [UIColor colorFromHexRGB:@"ffffff"];
    NSString * text = SESF(@"%02ld", (long)row);
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:30];
    label.textColor = color;
    label.text = text;
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (pickerView == self.leftPicker) {
        self.selectHour = row;
    }
    else if (pickerView == self.rightPicker) {
        self.selectMin = row;
    }
    [pickerView reloadComponent:component];
}
@end
